using AdmissionQA.Application.Dtos.Issue;
using AdmissionQA.Application.ServiceInterfaces;
using Microsoft.AspNetCore.Mvc;

namespace AdmissionQA.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class IssuesController(IIssueServices issueServices) : ControllerBase
{
    private readonly IIssueServices _issueServices = issueServices;

    [HttpGet("all")]
    public async Task<IActionResult> GetAll()
    {
        var result = await _issueServices.GetAllIssuesAsync();
        return Ok(result);
    }

    [HttpGet("paged/{pageIndex}/{pageSize}")]
    public async Task<IActionResult> GetPaged(int pageIndex = 0, int pageSize = 10)
    {
        var result = await _issueServices.GetPagedIssuesAsync(pageIndex, pageSize);
        return Ok(result);
    }

    [HttpGet("search")]
    public async Task<IActionResult> Search(string question)
    {
        var result = await _issueServices.SearchIssuesByQuestionAsync(question);
        return Ok(result);
    }

    [HttpGet("count")]
    public async Task<IActionResult> GetCount()
    {
        var result = await _issueServices.GetIssueCountAsync();
        return Ok(result);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var result = await _issueServices.GetIssueByIdAsync(id);
        return Ok(result);
    }

    [HttpPost("one")]
    public async Task<IActionResult> Create(IssueDto issue)
    {
        var result = await _issueServices.CreateIssueAsync(issue);
        return Ok(result);
    }

    [HttpPost("many")]
    public async Task<IActionResult> CreateMany(IssuesDto issues)
    {
        var result = await _issueServices.CreateIssuesAsync(issues);
        return Ok(result);
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, IssueDto issue)
    {
        var result = await _issueServices.UpdateIssueAsync(id, issue);
        return Ok(result);
    }

    [HttpDelete("one/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _issueServices.DeleteIssueAsync(id);
        return Ok(result);
    }

    [HttpDelete("many")]
    public async Task<IActionResult> DeleteMany(IEnumerable<Guid> ids)
    {
        var result = await _issueServices.DeleteIssuesAsync(ids);
        return Ok(result);
    }

    /// <summary>
    /// 语义搜索 - 基于向量相似度的智能问答
    /// </summary>
    /// <param name="query">用户查询文本</param>
    /// <param name="topK">返回最相似的前K个结果，默认5个</param>
    /// <returns>最相关的问答结果</returns>
    [HttpGet("semantic-search")]
    public async Task<IActionResult> SemanticSearch([FromQuery] string query, [FromQuery] int topK = 5)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("查询文本不能为空");
        }
        
        var result = await _issueServices.SearchIssuesByEmbeddingAsync(query, topK);
        return Ok(result);
    }

    /// <summary>
    /// 智能问答 - 返回最相关的单个答案
    /// </summary>
    /// <param name="query">用户问题</param>
    /// <returns>最相关的答案</returns>
    [HttpGet("smart-qa")]
    public async Task<IActionResult> SmartQA([FromQuery] string query)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("问题不能为空");
        }
        
        var result = await _issueServices.SearchIssuesByEmbeddingAsync(query, 1);
        if (result.Data != null && result.Data.Any())
        {
            var bestMatch = result.Data.First();
            return Ok(new
            {
                Question = query,
                Answer = bestMatch.Answer,
                RelatedQuestion = bestMatch.Question,
                Confidence = "高", // 可以后续添加置信度计算
                Source = "智能问答系统"
            });
        }
        
        return Ok(new
        {
            Question = query,
            Answer = "抱歉，我暂时没有找到相关的答案。请尝试换个方式提问，或者联系客服获取帮助。",
            Confidence = "低",
            Source = "智能问答系统"
        });
    }
}
